﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VFCons
{
	public partial class FTBinControl : UserControl
	{
		public EventHandler Buttons_Clicked;

		private ushort _freq = 0;
		public ushort Freq
		{
			get { return _freq; }
			set
			{
				_freq = value;
				textBoxFreq.Text = (_freq * 0.1F).ToString("F1");
				Invalidate();
			}
		}

		private ushort _volt = 0;
		public ushort Volt
		{
			get { return _volt; }
			set { _volt = value; textBoxVoltage.Text = (_volt * 0.1F).ToString("F1"); Invalidate(); }
		}

		private bool _binOpening = false;
		public bool BinOpeing
		{
			get { return _binOpening; }
			set { _binOpening = value; buttonOnOff.BackColor = value ? Color.Green : SystemColors.Control; }
		}

		public FTBinControl()
		{
			InitializeComponent();
			labelFreq.Text = "料仓频率" + Environment.NewLine + "(10.0~400.0)Hz";
			buttonOnOff.Tag = 0;
			buttonSet.Tag = 1;

			Freq = 10;
			Volt = 240;
			BinOpeing = false;

			buttonOnOff.Click += Buttons_Click;
			buttonSet.Click += buttonSet_Click;
		}

		private void buttonSet_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(textBoxFreq.Text))
			{
				MessageBox.Show("频率的有效值是(10.0~400.0)Hz", "Error");
				textBoxFreq.Focus();
				return;
			}
			ushort f = _freq;
			ushort v = _volt;
			try
			{
				_freq = Convert.ToUInt16(Convert.ToSingle(textBoxFreq.Text) * 10);
			}
			catch (Exception ex)
			{
				MessageBox.Show("频率的有效值是(10.0~400.0)Hz" + Environment.NewLine + ex.Message, "Error");
				textBoxFreq.Focus();
				textBoxFreq.SelectAll();
			}
			try
			{
				_volt = Convert.ToUInt16(Convert.ToSingle(textBoxVoltage.Text) * 10);
			}
			catch (Exception ex)
			{
				MessageBox.Show("电压的有效值是(0.0~24)V" + Environment.NewLine + ex.Message, "Error");
				textBoxVoltage.Focus();
				textBoxVoltage.SelectAll();
				return;
			}

			Buttons_Click(sender, e);

		}

		private void Buttons_Click(object sender, EventArgs e)
		{
			if (Buttons_Clicked != null) Buttons_Clicked(sender, e);
		}
	}
}
